class Foo {
public:
    Foo()
    {}

    void first(function<void()> printFirst)
    {
        unique_lock<mutex> lock(_lock);
        while (choose != 1)
            _cv.wait(lock);

        printFirst();
        choose = 2;
        _cv.notify_all();
    }

    void second(function<void()> printSecond)
    {
        unique_lock<mutex> lock(_lock);
        while (choose != 2)
            _cv.wait(lock);

        printSecond();
        choose = 3;
        _cv.notify_all();
    }

    void third(function<void()> printThird)
    {
        unique_lock<mutex> lock(_lock);
        while (choose != 3)
            _cv.wait(lock);

        printThird();
        choose = 1;
        _cv.notify_all();
    }

private:
    mutex _lock;
    condition_variable _cv;
    int choose = 1;
};